package stack;

//扩展功能
public class ArrayStack2 {
    private int[] array; //存储元素的数组
    private int top; //栈顶的指针
    private int capacity; //栈的最大容量

    //构建方法，创建一个指定容量的栈
    public ArrayStack2(int capacity) {
        this.array = new int[capacity];
        this.top = -1;  //栈为空
        this.capacity = capacity;
    }

    //出栈

    public int pop() {
        //判断栈是否为空
        if (ifEmpty()) {
            throw new RuntimeException("stack is Empty");
        }
        //返回栈顶元素，同时指针-1
        return array[top--];
    }

    //获取栈顶元素，但是不删除
    public int peek() {
        //判断栈是否为空
        if (ifEmpty()) {
            throw new RuntimeException("stack is Empty");
        }
        return array[top];
    }

    //入栈方法 将元素推送到栈顶
    public void push(int item) {
        if (isFull()) { //判断栈是否已经满了
            throw new RuntimeException("stack is full");
        }
        //将元素添加到栈顶,同时指针+1
        array[++top] = item;
    }

    //判断栈是否为空
    public boolean ifEmpty() {
        return top == -1; //栈的指针-1，表示栈为空
    }

    //判断栈是否满了
    public boolean isFull() {
        return top == capacity - 1;//栈顶指针+1==栈容量 表示栈满了
    }

    //返回栈中元素的数量
    public int size() {
        return top + 1;//栈顶指针+1就是数量
    }
    public void clear() {
        top = -1; //栈指针-1，表示栈为空
    }
    //显示栈
    public void list(){
        if (ifEmpty()) {
            System.out.println("栈为空");
        }
        for (int i = top; i >=0 ; i++) {
            System.out.printf("start[%d]=%d\n",i,array[i]);
        }

    }
    //返回运算符的优先级 优先级使用数字表示，数字越大优先级越高
    public int priority(String oper) {
        if (oper == "*" || oper == "/") {
            return 1;
        } else if (oper == "+" || oper == "-") {
            return 0;
        } else {
            return -1;
        }
    }

    //判断是否为数字
    public boolean isOper(String val) {
        return val == "*" || val == "-" || val == "/" || val == "+";
    }

    //计算方法
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
            case '-':
                res = num2 - num1;
            case '*':
                res = num2 * num1;
            case '/':
                res = num2 / num1;
            default:
                break;
        }
        return res;
    }

}
